package com.ruoyi.web.sso.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.sso.domain.Group;
import com.ruoyi.web.sso.domain.Staff;
import com.ruoyi.web.sso.service.IGroupService;
import com.ruoyi.web.sso.service.IStaffService;
import com.ruoyi.web.sso.util.DateUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * <p>
 * 集团信息 前端控制器
 * </p>
 *
 * @author cdd
 * @since 2021-10-21
 */
@Controller
@RequestMapping("/sso/group")
@Api(tags = "集团信息")
public class GroupController extends BaseController {

    private String prefix = "sso/group";


    @Autowired
    private IGroupService groupService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IStaffService staffService;

    @GetMapping()
    @ApiOperation(value="查询列表页面",notes="查询列表页面")
    public String group()
    {
        return  "sso/group/group";
    }

    /**
     * 查询集团信息列表
     */
    @PostMapping("/list")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = Group.class),
    })
    @ApiOperation(value="查询集团信息列表",notes="查询集团信息列表")
    public TableDataInfo list(Group group)
    {
        startPage();
        List<Group> list = groupService.selectGroupList(group);
        return getDataTable(list);
    }

    /**
     * 导出集团信息列表
     */
    @Log(title = "集团信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    @ApiOperation(value="导出集团信息列表",notes="导出集团信息列表")
    public AjaxResult export(Group group)
    {
        List<Group> list = groupService.selectGroupList(group);
        ExcelUtil<Group> util = new ExcelUtil<Group>(Group.class);
        return util.exportExcel(list, "集团信息数据");
    }

    /**
     * 新增集团信息
     */
    @GetMapping("/add")
    @ApiOperation(value="新增页面",notes="新增页面")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存集团信息
     */
    @Log(title = "集团信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation(value="新增集团信息",notes="新增集团信息")
    public AjaxResult addSave(Group group)
    {
        return groupService.add(group);
    }

    /**
     * 修改集团信息
     */
    @GetMapping("/edit/{id}")
    @ApiOperation(value="修改页面",notes="修改页面")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        Group group = groupService.selectGroupById(id);
        mmap.put("group", group);
        return prefix + "/edit";
    }

    /**
     * 修改保存集团信息
     */
    @Log(title = "集团信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    @ApiOperation(value="修改集团信息",notes="修改集团信息")
    public AjaxResult editSave(Group group)
    {
        return groupService.updateGroup(group);
    }

    /**
     * 删除集团信息
     */
    @Log(title = "集团信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    @ApiOperation(value="删除集团信息",notes="删除集团信息")
    public AjaxResult remove(String ids)
    {
        return toAjax(groupService.deleteGroupByIds(ids));
    }


    @GetMapping("/selectGroupUserList/{id}")
    @ApiOperation(value="集团管理员列表",notes="集团管理员列表")
    public String selectGroupUserList(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("id", id);
        return prefix + "/groupUserList";
    }


    @PostMapping("/selectGroupStaffList")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = Group.class),
    })
    @ApiOperation(value="查询集团信息列表",notes="查询集团信息列表")
    public TableDataInfo selectGroupStaffList(String groupId,String mobile)
    {
        startPage();
        List<Staff> list = staffService.selectGroupStaffList(groupId,mobile);
        return getDataTable(list);
    }

    @GetMapping("/selectUser/{id}")
    @ApiOperation(value="用户列表",notes="用户列表")
    public String selectUser(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("id", id);
        return prefix + "/selectUser";
    }

    /**
     * 批量选择用户授权
     */
    @PostMapping("/selectGroupUserAll")
    @ResponseBody
    public AjaxResult selectGroupUserAll(String groupId, String[] staffIds)
    {
        return toAjax(groupService.insertAuthUsers(groupId, staffIds));
    }

    /**
     * 批量取消用户授权
     */
    @PostMapping("/cancelAll")
    @ResponseBody
    public AjaxResult cancelAll(String groupId,  String userIds)
    {
        return toAjax(groupService.cancelAll(groupId, userIds));
    }

    public static void main(String[] args) {
        double a = betweenMonthsV2(DateUtil.stringByYMD("2022-05-26"),DateUtil.stringByYMD("2022-05-25"));
        System.out.println((int)Math.ceil( a *28.5) );
    }

    /**
     * 精确的月份
     *
     * @param bigger
     * @param smaller
     * @return
     */
    public static final double betweenMonthsV2(Date bigger, Date smaller) {
        if (bigger.compareTo(smaller) < 0)
            return 0;
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(bigger);
        c2.setTime(smaller);
        Integer year = c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
        int month = c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
        int days = c1.get(Calendar.DAY_OF_MONTH) - c2.get(Calendar.DAY_OF_MONTH);

        double daysOfMonth = getDayCountOfMonth() * 1.0;

        double monthDelta = days * 1.0 / daysOfMonth;

        double ret = month + (year * 12) + monthDelta;

        return ret;
    }
    private static int getDayCountOfMonth() {
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        return day;
    }

    //获取当前时间上个月最后一天
    public static final Date getEndOfLastMonth(Date day){
        Calendar c = Calendar.getInstance();
        c.setTime(day);
        c.add(Calendar.MONTH, -1);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));//设置为1号,当前日期既为本月第一天
        c.set(Calendar.HOUR, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        return c.getTime();
    }

}
